home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16624 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.1 KB

  1. Path: orion.hrz.tu-freiberg.de!wirbel
  2. From: wirbel@orion.hrz.tu-freiberg.de (Frank Wirbeleit)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: returning an array from function
  5. Followup-To: comp.lang.c,comp.lang.c++
  6. Date: 11 Apr 1996 13:46:47 GMT
  7. Organization: BA Freiberg
  8. Message-ID: <4kj2g7$fem@kermes.hrz.tu-freiberg.de>
  9. References: <4jstd8$kh0@news1.sunbelt.net> <4juigtINNrus@keats.ugrad.cs.ubc.ca> <4jv9gf$m7k@news.microsoft.com>
  10. NNTP-Posting-Host: orion.hrz.tu-freiberg.de
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Hi,
  14.  
  15. I have created a lib for microsoft and borland compiler to handle
  16. arrays of any size and also to return from a function. Please try
  17. the ftp : schuetz.exphys.tu-freiberg.de in the directory pub/matrix/matrix_lib
  18. is a packed file located.
  19.  
  20. Frank
  21.  
  22. wirbel@physik.tu-freiberg.de
  23.  
  24. Dann Corbit (a-cnadc@microsoft.com) wrote:
  25. : In article <4juigtINNrus@keats.ugrad.cs.ubc.ca>, c2a192@ugrad.cs.ubc.ca says...
  26. : >
  27. : >In article <4jstd8$kh0@news1.sunbelt.net>,
  28. : >Rick Huebner <bourne@infoave.net> wrote:
  29. : >>I have looked in several texts and looked through the faq for this question
  30. : >>and can't find a reference for my answer.  I am trying to return an array, or
  31. : >
  32. : >The FAQ discusses arrays and pointers at length, something that is crucial to
  33. : >C. I suspect you are scanning through it for an example of an array being
  34. : >passed to and returned from a function. However, a careful reading will teach
  35. : >you that this cannot be done.
  36. : >
  37. : >You cannot return arrays from functions nor pass them into functions. Nor can
  38. : >you assign directly to arrays (you can _initialize_ arrays but initialization
  39. : >is different from assignment). 
  40. : >
  41. : >>a pointer to the array, from a function.  I have seen several examples of
  42. : >>returning a pointer, but it doesn't seem to work for me.  My function is going
  43. : >>to return an array of integers where 1-80 of them will be significant.  The
  44. : >>calling function will know how many integers are expected and will read them
  45. : >>off the array....If I can get the pointer back to the array.  A friend of mine
  46. : >>told me to just make the array a global, but I don't want to do that if I can
  47. : >>help it.  I would even resort to a recursive function that will pass the
  48. : >>integers back one at a time if that is possible.  Does anyone have any
  49. : >>suggestions?  I am open to anything and will go looking if you can tell me
  50. : >>where(hopefully online somewhere).
  51. : >
  52. : >It's not clear what you want. If you want the function to do something to your
  53. : >array, why would you care about the pointer? Its value is not surprising---it
  54. : >is just the address of element 0.  
  55. : >
  56. : >The only reason for wanting to pass arrays by value to a function would be to
  57. : >have the function operate on its own _copy_ of the array rather than the
  58. : >original object. C doesn't have this, but it does allow structure passing. If
  59. : >you make your array part of a structure, you can do it:
  60. : >
  61. : >        struct myarray {
  62. : >                int x[40];
  63. : >        }
  64. : >
  65. : >        int compute_something(struct myarray M)
  66. : >
  67. : >        {
  68. : >                /* do something with M.x[]      */
  69. : >
  70. : >                return 0;
  71. : >        }
  72. : >
  73. : >The function compute_something() gets its own copy of the structure, and
  74. : >therefore its own private copy of the array to work on. This can be wasteful,
  75. : >especially if the function doesn't really need a private copy of the array.
  76. : >
  77. : >If you want to protect yourself against accidentally modifying an array in a
  78. : >function that isn't supposed to, try using ``const'':
  79. : >
  80. : >        int compute_something(const int arr[]);
  81. : >
  82. : The only time there is benefit from passing a struct containing an array 
  83. : by value is to modify the contents of the array in the called function,
  84. : but prevent the array from being changed in the calling routine.
  85.  
  86. : Otherwise (for read only data) the const modifier is a much better way to go
  87. : (as demonstrated above).
  88. : -- 
  89. : The opinions expressed in this message are my own personal views
  90. : and do not reflect the official views of Microsoft Corporation.
  91. : In fact, I'm just a subcontractor, not an employee, so pull in your claws.
  92.  
  93.